package com.espirit.builder.biz;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.espirit.common.WFMessage;
import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.manager.RelatedModel;
import com.espirit.eap.manager.TabbedModel;
import com.espirit.eap.manager.workflow.FormMessage;
import com.espirit.eap.manager.workflow.WFInstanceTracker;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.Model;
import com.espirit.eap.pagelayout.ModelField;
import com.googlecode.cswish.annotation.Dependence;

@Dependence({
	"com.espirit.builder.biz.WFMessageBuilder",
	"com.espirit.builder.biz.FormBuilder"
})
@Component
public class FormMessageBuilder extends ModelBuilderSupport {
	static final String CREATED = "form.created";
	static final String CREATED_ON = "form.createdOn";
	static final String ENTITY_STATUS = "form.entityStatus";
	static final String STATUS_TYPE="message.statusType";
	static final String FROM = "message.from";
	static final String TO = "message.to";
	static final String INIT_DATE = "message.initDate";
	static final String TITLE = "message.title";
	static final String PHASE = "message.instance.wfPhase";
	static final String DESCRIPTION = "message.description";
	static final String SRC_CLASSNAME = "message.instance.srcClassName";
	
	FunctionButton approve = null;
	
	@Override
	public boolean init() {
		return setClass(FormMessage.class, "Workflow Process", "流程审批");
	}
	
	@Override
	public void buildFields() {
		approve = createFunctionButton("_approve", "Approve", "审核",this.clazz,"_approve",false,null,null);
		addModelField(FROM, "From", "发起者");
		addModelField(TO, "To", "接收者");
		addModelField(INIT_DATE, "Init Date", "发起时间");
		addModelField(TITLE, "Title", "标题");
		addModelField(PHASE, "Current Phase", "当前环节");
		addModelField(SRC_CLASSNAME, "Type", "表单类型");
	}
	
	
	@Override
	protected void addSearchFields() {
		super.addSearchField(convertPropertyToField(SRC_CLASSNAME, STATUS_TYPE));
	}
	
	@Override
	protected void addListFields() {
		addListField(convertPropertyToField(STATUS_TYPE, FROM, INIT_DATE, TO,  TITLE, PHASE, CREATED, CREATED_ON));
	}
	
	@Override
	protected void addDetailFields() {
		addDetailField(convertPropertyToField(FROM, INIT_DATE, TO, STATUS_TYPE, TITLE, PHASE, CREATED, CREATED_ON));
	}
	
	@Override
	protected void setDefaultLayoutButtons() {
		addButton(approve);
		addButton(FunctionButton.EXPORT);
	}
	
	protected RelatedModel[] getRelatedModels(){
		RelatedModel getLayout = new RelatedModel(this.clazz, "getLayout");
		return new RelatedModel[]{getLayout};
	}
	
	protected TabbedModel[] getTabbedModels(){
		TabbedModel approveInfo = new TabbedModel(WFInstanceTracker.class,"searchRelatedResult");
		return new TabbedModel[]{approveInfo};
	}

	@Override
	protected void afterBuilderLayout() {
		Model wfMessage = getModel(WFMessage.class);
		bindButtonToLayout(approve, wfMessage.getDefaultDetailLayout());
		
		super.afterBuilderLayout();
	}
	
	@Override
	protected Element convertFieldToElement(ModelField mf, boolean isListLayout) {
		Element element = super.convertFieldToElement(mf, isListLayout);
		String name = element.getName();
		if (isListLayout){
			if (name.equals(SRC_CLASSNAME)){
				Map<String, String> exDataTypeProperties = new HashMap<String, String>();
				exDataTypeProperties.put("collection", "fn.service('" + modelService.getModelName(FormMessage.class) + "').srcClassNames");
				element.setExProperty(exDataTypeProperties);
				element.setTagName("select");
			}
			if (name.equals(STATUS_TYPE)){
				Map<String, String> exDataTypeProperties = new HashMap<String, String>();
				exDataTypeProperties.put("collection", "fn.service('" + modelService.getModelName(WFMessage.class) + "').statusType");
				element.setTagName("select");
				element.setExProperty(exDataTypeProperties);
			}
		}
		
		return element;
	}
}
